import pytest
import drc_core_python as drc

def test_create_schematic():
    schematic = drc.Schematic()
    assert schematic is not None
    assert schematic.name == ""
    assert len(schematic.components) == 0
    assert len(schematic.nets) == 0

def test_power_net_check():
    schematic = drc.Schematic()
    schematic.name = "Test Circuit"
    
    # 创建VDD网络（错误类型）
    vdd_net = drc.Net()
    vdd_net.name = "VDD"
    vdd_net.net_type = "signal"
    schematic.nets.append(vdd_net)
    
    # 创建DRC管理器并运行检查
    drc_manager = drc.DRCManager()
    drc_manager.load_rule("rules/power_net_check.py")
    violations = drc_manager.run_all_checks(schematic)
    
    # 验证违规
    assert len(violations) > 0
    assert any(v.message == "GND network is missing" for v in violations)
    assert any("should be of type 'power'" in v.message for v in violations)

def test_component_naming():
    schematic = drc.Schematic()
    
    # 创建一个错误命名的组件
    bad_component = drc.Component()
    bad_component.name = "IC1"
    bad_component.type = "ic"
    schematic.components.append(bad_component)
    
    # 创建DRC管理器并运行检查
    drc_manager = drc.DRCManager()
    drc_manager.load_rule("rules/component_naming_check.py")
    violations = drc_manager.run_all_checks(schematic)
    
    # 验证违规
    assert len(violations) > 0
    assert any("should start with 'U'" in v.message for v in violations)

def test_pin_connections():
    schematic = drc.Schematic()
    
    # 创建一个没有连接的电源网络
    vdd_net = drc.Net()
    vdd_net.name = "VDD"
    vdd_net.net_type = "power"
    schematic.nets.append(vdd_net)
    
    # 创建DRC管理器并运行检查
    drc_manager = drc.DRCManager()
    drc_manager.load_rule("rules/power_net_check.py")
    violations = drc_manager.run_all_checks(schematic)
    
    # 验证违规
    assert len(violations) > 0
    assert any("must connect to at least two pins" in v.message for v in violations) 